//
//  CYHHotSecVC.m
//  Cat
//
//  Created by dllo on 15/11/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CYHHotSecVC.h"
#import "ReadTableViewCell.h"

@interface CYHHotSecVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *HotSecTableView;
@property (nonatomic, strong) NSMutableDictionary *HotSecDic;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *headerPic;
@property (nonatomic, copy) NSMutableArray *likeUids;
@property (nonatomic, strong) HotModel *likeModel;

@end

@implementation CYHHotSecVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self.openType.openType isEqualToString:@"web"]) {
        UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, -50, VIEW_WIDTH, VIEW_HEIGHT + 50)];
        [self.view addSubview:web];
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.openType.openValue]]];
        web.scrollView.bounces = NO;
    } else if ([self.openType.openType isEqualToString:@"dynamic"]) {
        [self createView];
        [self createData];
    }
}

#pragma mark - createView
- (void)createView {
    self.HotSecTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.HotSecTableView];
    self.HotSecTableView.delegate = self;
    self.HotSecTableView.dataSource = self;
    self.HotSecTableView.separatorStyle = NO;
    self.HotSecTableView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    
    [self.HotSecTableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"HotSec"];
}

#pragma mark - createData
- (void)createData {
    NSString *url = [NSString stringWithFormat:@"http://api.huapipi.com/dynamic/l1/detail/%@?deviceId=1826162&uid=1033268&w=720&loc=channelDetail&channelId=0&h=1280", self.openType.openValue];
    [ToolNetWorking ToolNetWorking:url block:^(id result) {
        self.HotSecDic = [NSMutableDictionary dictionary];
        self.HotSecDic = result[@"dynamic"];
        
        
        // 点赞头像
        self.likeModel = [[HotModel alloc] init];
        self.likeModel.likeUids = self.HotSecDic[@"likeUids"];
        [self.HotSecTableView reloadData];
        
        // 头像
        [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://user.huapipi.com/userinfo/basic.do?uid=%@", self.HotSecDic[@"uid"]] block:^(id result) {
            self.name = result[@"nickname"];
            self.headerPic = result[@"portrait"];
            [self.HotSecTableView reloadData];
        }];
    }];
}



#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotSec"];
    
    cell.wordLabel.text = self.HotSecDic[@"content"][@"word"];
    [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.headerPic] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
    cell.nameLabel.text = self.name;
    cell.likeLabel.text = [NSString stringWithFormat:@"%@", self.HotSecDic[@"likes"]];
    cell.commentsView.HeaderModel = self.likeModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
     // 根据文本字体大小来计算文本的cell高度
     // 第二个参数:
    CGRect rect = [self.HotSecDic[@"content"][@"word"] boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return  180 + rect.size.height;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
